package com.like.likeutils.util;

import java.io.File;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.DocumentsContract;
import android.provider.MediaStore;

public class PickFileUtil {
	public final static int REQUEST_TAKE_PHOTO = 0;
	public final static int REQUEST_FROM_FILE = 1;

	private static PickFileUtil mInstance;
	private Activity mActivity;
	private ContentResolver mResolver;

	public PickFileUtil(Activity activity) {
		mActivity = activity;
		mResolver = mActivity.getContentResolver();
	}

	@Deprecated
	public static PickFileUtil getInstance(Activity activity) {
		mInstance = new PickFileUtil(activity);
		return mInstance;
	}

	private File getFileByUri(Uri uri) {
		String tmpPicturePath = getPath(uri);
		if (tmpPicturePath != null)
			return new File(tmpPicturePath);
		return null;
	}

	@SuppressLint("NewApi")
	private String getPath(Uri uri) {
		if (uri == null) {
			return null;
		}
		String[] projection = { MediaStore.Images.Media.DATA };
		Cursor cursor;
		if (Build.VERSION.SDK_INT >= 19) {
			try {
				// Will return "image:x*"
				String wholeID = DocumentsContract.getDocumentId(uri);
				// Split at colon, use second item in the array
				String id = wholeID.split(":")[1];
				// where id is equal to
				String sel = MediaStore.Images.Media._ID + "=?";
				cursor = mResolver.query(
						MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
						projection, sel, new String[] { id }, null);
			} catch (Exception e) {
				cursor = mResolver.query(uri, projection, null, null, null);
			}
		} else {
			cursor = mResolver.query(uri, projection, null, null, null);
		}
		String path = null;
		try {
			int column_index = cursor
					.getColumnIndex(MediaStore.Images.Media.DATA);
			cursor.moveToFirst();
			path = cursor.getString(column_index).toString();
			cursor.close();
		} catch (NullPointerException e) {

		}
		return path;
	}

	private Uri getUriByData(Intent data) {
		Uri uri = data.getData();
		if (uri == null) {
			Bundle extras = data.getExtras();
			Bitmap bitmap = (Bitmap) extras.get("data");
			uri = Uri.parse(MediaStore.Images.Media.insertImage(mResolver,
					bitmap, null, null));
		}
		return uri;
	}

	public File getFileByData(Intent data) {
		Uri uri = getUriByData(data);
		return getFileByUri(uri);
	}

	public void takePhoto(File photoFile) {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
		if (photoFile != null)
			intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
		mActivity.startActivityForResult(intent, REQUEST_TAKE_PHOTO);
	}

	public void takePhoto(File photoFile, int request) {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
		if (photoFile != null)
			intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
		mActivity.startActivityForResult(intent, request);
	}

	public void fromFile() {
		Intent intent = new Intent();
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		mActivity.startActivityForResult(intent, REQUEST_FROM_FILE);
	}

	public void fromFile(int request) {
		Intent intent = new Intent();
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		mActivity.startActivityForResult(intent, request);
	}

}
